const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const moment = require('../../../lib/moment/moment.min.js');
var app = getApp();
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bill:{},
    billNo:'',
    isLoad:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    if (options.billNo){
      that.setData({
        billNo: options.billNo
      });
      that.initDetail();
    }else{
      util.toast.fail('房租明细不存在!');
    }
  },
  initDetail(){
    app.postForm(api.GetRentDetail, { billNo: that.data.billNo}).then(rsp => {
      var detail = rsp.data;
      if (detail){
        Object.keys(detail).forEach(function(k,n){
          if (['totalCost', 'rentAmount', 'networkFee', 'electricity', 'waterFee', 'hygieneFee','otherFees'].indexOf(k) != -1){
            detail[k] = (Number(detail[k])/100).toFixed(2);
          }
        });
        //标题时间
        detail.tltDate = moment(detail.createTime).format('YYYY年M月');
        //房租周期
        detail.rentCycle = moment(detail.createTime).format('YYYY年MM月DD日') + '-' + moment(detail.arrival).format('M月DD日');
        //出账日
        detail.accDate = moment(detail.createTime).format('YYYY年MM月DD日');
        //交租日
        detail.cycle = moment(detail.cycle).format('YYYY年MM月DD日');
        that.setData({
          isLoad:true,
          bill:detail
        });
      }
    }).catch((err) => {
      console.log(err);
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    that.initDetail();
    wx.stopPullDownRefresh();
  },

})